package com.lecture.java8.datastructure;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/4/7 16:28
 */
public class ArrayStack {
    private String[] item;//数组
    private int count;//栈的元素个数
    private int n;//栈的大小

    //申请一个大小为n的栈
    public ArrayStack(int n) {
        this.item = new String[n];
        this.count = 0;
        this.n = n;
    }

    //入栈
    public boolean push(String item) {
        //判断栈是否有空余空间
        if (count >= n) {
            return false;
        }
        //将item放入最前面的位置
        this.item[count] = item;
        count ++;
        return true;
    }

    //出栈
    public String pop() {
        //判断栈是否为空
        if (count == 0) {
            return null;
        }
        //返回count-1的元素 并且count--
        String item = this.item[count - 1];
        count --;
        return item;
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(10);
        stack.push("A");
        stack.push("B");
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}
